<template>
    <div>
        <div id="slider" class="mui-slider">
            <div id="sliderSegmentedControl" class="mui-scroll-wrapper mui-slider-indicator mui-segmented-control mui-segmented-control-inverted">
                <div class="mui-scroll">                    
                    <!-- <router-link class="mui-control-item mui-active" to="" v-for="item in categories" :key="item.id">{{ item.title }}</router-link>                     -->
                    <!-- 使用 click 事件，手机中可能会有问题，改用 tap （MUI中的事件） -->
                    <a :class="['mui-control-item', item.id == 0 ? 'mui-active' : '']"
                        v-for="item in categories" :key="item.id" @tap="getImageList(item.id)">
                        {{ item.title }}
					</a>
                </div>
            </div>

		</div>

        <ul class="photo-image">
            <router-link v-for="item in images" :key="item.id" :to="'/home/photoinfo/' + item.id" tag="li">
                <img v-lazy="item.img_url">
            
                <div class="info">
                    <h1 class="info-title">{{ item.title }}</h1>
                    <div class="info-content">{{ item.zhaiyao }}</div>
                </div>
            </router-link>            
        </ul>
    </div>
</template>


<script>
// 引用会导致严格模式错误，需要使用第三方组件(babel-plugin-transform-remove-strict-mode)
// 去除 webpack 用严格方式打包
import mui from '../../lib/mui/js/mui.min.js'

// 不能放在这里初始化，会导致第一次进入页面无法滑动滚动条
// mui('.mui-scroll-wrapper').scroll({
// 	deceleration: 0.0005 //flick 减速系数，系数越大，滚动速度越慢，滚动距离越小，默认值0.0006
// });

export default {
    data() {
        return {
            categories: [],
            images: []
        };
    },
    mounted() {
        // 解决第一次进入页面无法滑动bug
        mui('.mui-scroll-wrapper').scroll({
            deceleration: 0.0005 //flick 减速系数，系数越大，滚动速度越慢，滚动距离越小，默认值0.0006
        });

        this.getImgCategory();
        this.getImageList(0);
    },
    methods: {
        // 获取图片分类
        getImgCategory() {
            this.$http.get('api/getimgcategory')
            .then(result => {
                if (result.body.status === 0) {
                    // 获取成功                    
                    this.categories = result.body.message;
                    this.categories.unshift({
                        title: "全部",
                        id: 0
                    })
                }
            })            
        },
        // 获取图片列表
        getImageList(id) {
            this.$http.get('api/getimages/' + id)
            .then(result => {
                if (result.body.status === 0) {
                    // 查询成功
                    this.images = result.body.message;
                }
            })
        }
    }
}
</script>


<style lang="scss" scoped>
* {
    // 去除滑动警告 
    // [Intervention] Unable to preventDefault inside passive event listener due to target being treated as passive. See <URL>

    touch-action: pan-y;
}



.photo-image {
    list-style: none;
    
    margin: 0;
    padding: 10px;
    padding-bottom: 0;

    li {
        background-color: #ccc;
        text-align: center;      
        margin-bottom: 10px;  
        box-shadow: 0 0 9px #999;
        position: relative;

        img {
            width: 100%;    
            vertical-align: middle;
        }

        img[lazy=loading] {
            width: 40px;
            height: 300px;
            margin: auto;
        }

        .info {
            color: white;
            position: absolute;
            bottom: 0;
            max-height: 84px;
            background-color: rgba(0, 0, 0, 0.4);
            .info-title {
                font-size: 14px;
            }
            .info-content {
                font-size: 13px;
            }

        }
    }
}

</style>
